#include "CSettingsWnd.h"

CSettingsWnd::CSettingsWnd(QWidget *p) : QFrame(p) {
    initUI();
    initMainWnd();
    setObjectName("SettingsWnd");
    _isRecord = false;
    qApp->installEventFilter(this);
}

CSettingsWnd::~CSettingsWnd() {}

void CSettingsWnd::initUI() {
    _hideBtn = new QPushButton(tr("Back"), this);
    _hideBtn->setObjectName("SettingsWnd_HideBtn");
    _hideBtn->setMaximumHeight(40);
    _mainWnd = new QGroupBox(this);
    _mainWnd->setObjectName("SettingsWnd_MainFram");
    QVBoxLayout *vb = new QVBoxLayout;
    vb->addWidget(_hideBtn);
    vb->addWidget(_mainWnd);
    setLayout(vb);
    connect(_hideBtn, &QPushButton::clicked, [&] { emit turnBack(); });
}

void CSettingsWnd::initMainWnd() {
    _recordKeyEdit = new QLineEdit(this);
    _recordKeyEdit->setEnabled(false);
    _modifyKeyBtn = new QPushButton(tr("Modify"), this);
    QLabel *lab1 = new QLabel(tr("Hot key"), this);

    QGridLayout *gd = new QGridLayout;
    gd->addWidget(lab1, 0, 0, 1, 2);
    gd->addWidget(_recordKeyEdit, 1, 0, 1, 1);
    gd->addWidget(_modifyKeyBtn, 1, 1, 1, 1);
    _mainWnd->setLayout(gd);

    connect(_modifyKeyBtn, &QPushButton::clicked, [&] {
        _isRecord ? _modifyKeyBtn->setText(tr("Modify")) : _modifyKeyBtn->setText(tr("Confirm"));
        _isRecord = !_isRecord;
        _haveFuncBtn = false;
    });
}

void CSettingsWnd::keyPressEvent(QKeyEvent *evt) {
    if (!_isRecord) {
        return QFrame::keyPressEvent(evt);
    }
    qDebug() << evt->modifiers();
    qDebug() << evt->key();
    qDebug() << evt->text();

    if (evt->modifiers() == Qt::NoModifier) {
        _haveFuncBtn = false;
        _recordKeyEdit->setText(tr("You must bind one function key."));
        return;
    } else if (evt->modifiers() == Qt::KeypadModifier) {
        // _recordKeyEdit->setText(getModifersKeyName(evt->modifiers()) + "  +  "  + QString::number(evt->key()));
        return;
    } else {
        _haveFuncBtn = true;
        _recordKeyEdit->setText(getModifersKeyName(evt->modifiers()));
    }

    if (_haveFuncBtn) {
        if (evt->modifiers() == Qt::KeypadModifier) {
            return;
        }
        QString tmpText = "";
        if (evt->key() >= Qt::Key_Shift && evt->key() <= Qt::Key_ScrollLock) {
            return;
        }
        if (evt->key() >= Qt::Key_F1 && evt->key() <= Qt::Key_F35) {
            tmpText = "F" + QString::number(evt->key() - Qt::Key_F1 + 1);
        } else {
            char a = evt->key();
            tmpText = QString(a);
        }

        _recordKeyEdit->setText(_recordKeyEdit->text() + "  +  " + tmpText);
        _haveFuncBtn = false;
    }
}

QString CSettingsWnd::getModifersKeyName(int v) {
    if (v == Qt::ControlModifier) {
        return "Control";
    } else if (v == Qt::ShiftModifier) {
        return "Shift";
    } else if (v == Qt::AltModifier) {
        return "Alt";
    } else if (v == Qt::KeypadModifier) {
        return "Keypad";
    } else if (v == Qt::GroupSwitchModifier) {
        return "GroupSwitch";
    } else if (v == Qt::KeyboardModifierMask) {
        return "Keyboard";
    }

    return "Unknown";
}